package cl.kaisanetwork.kaisaportal.page.accounts;

import java.util.Date;

import org.apache.click.control.FieldSet;
import org.apache.click.control.Form;
import org.apache.click.control.Label;
import org.apache.click.control.Submit;
import org.apache.click.control.TextField;
import org.apache.click.extras.control.EmailField;
import org.apache.click.util.Bindable;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import cl.kaisanetwork.kaisaportal.domain.entities.Usuario;
import cl.kaisanetwork.kaisaportal.page.BorderPage;
import cl.kaisanetwork.kaisaportal.page.home.Home;

@Component
public class SignUp extends BorderPage {

	private static final long serialVersionUID = 1L;

	@Bindable
	protected Form form = new Form();

	// User information Fields
	private Label lbUserName;
	private TextField txtNombres;
	private TextField txtApellidos;
	private TextField txtCargo;
	private TextField txtEmpresa;
	private Label lbEmail;
	private EmailField txtAltEmail;
	private TextField txtTelefono;
	private TextField txtMovil;

	public SignUp() {
		
		form.setLabelAlign(Form.ALIGN_LEFT);
		form.setLabelStyle("width:11em;");
		form.setFieldStyle("width:22em;");

		FieldSet accountInfo = new FieldSet("accountInfo", "Datos Basicos");
		
		lbUserName = new Label("lbUserName", "Nombre Usuario:");
		txtNombres = new TextField("txtNombres", "Nombres:", 25, false);
		txtApellidos = new TextField("txtApellidos", "Apellidos:", 25, false);
		txtCargo = new TextField("txtCargo", "Cargo:", 25, false);
		txtEmpresa = new TextField("txtEmpresa", "Empresa:", 25, false);

		accountInfo.add(lbUserName);
		accountInfo.add(txtNombres);
		accountInfo.add(txtApellidos);
		accountInfo.add(txtCargo);
		accountInfo.add(txtEmpresa);

		form.add(accountInfo);

		FieldSet contactInfo = new FieldSet("contactInfo", "Datos de Contacto");

		txtAltEmail = new EmailField("txtAltEmail", "Email Alternativo:", 25, false);
		txtTelefono = new TextField("txtTelefono", "Telefono:", 25, false);
		txtMovil = new TextField("txtMovil", "Tel. Movil:", 25, false);
		lbEmail = new Label("lbEmail", "Email");

		contactInfo.add(lbEmail);
		contactInfo.add(txtAltEmail);
		contactInfo.add(txtTelefono);
		contactInfo.add(txtMovil);

		form.add(contactInfo);
		
		form.add(new Submit("btnSave", "Guardar", this, "onSubmit"));
	}

	@Override
	public void onInit() {
		super.onInit();
		setNavMenuSelected(BorderPage.CONTROLPANEL);
		setTextTopBox("Sign Up");
	}
	
	@Override
	public boolean onSecurityCheck() {
		return true;
	}
	
	@Override
	public void onGet() {
		if(!StringUtils.isEmpty(googleUser.getNickname())) {
			lbUserName.setValue(googleUser.getNickname());
		}
		if(!StringUtils.isEmpty(googleUser.getEmail())) {
			lbEmail.setValue(googleUser.getEmail());
		}
	}
	
	public boolean onSubmit() {
		if(!form.isValid()) {
			return false;
		}
		Date currentDate = new Date();
		String currentIp = getContext().getRequest().getRemoteAddr();
		
		if(currentUser == null) {
			currentUser = new Usuario();
			currentUser.setNombres(txtNombres.getValue());
			currentUser.setApellidos(txtApellidos.getValue());
			currentUser.setCargo(txtCargo.getValue());
			currentUser.setEmpresa(txtEmpresa.getValue());
			currentUser.setAlt_email(txtAltEmail.getValue());
			currentUser.setTelefono(txtTelefono.getValue());
			currentUser.setMovil(txtMovil.getValue());
			currentUser.setLast_request_at(currentDate);
			currentUser.setLast_login_at(currentDate);
			currentUser.setCurrent_login_at(currentDate);
			currentUser.setLast_login_ip(currentIp);
			currentUser.setCurrent_login_ip(currentIp);
			currentUser.setLogin_count(1);
			currentUser.setCreated_at(currentDate);
			currentUser.setUpdated_at(currentDate);
			currentUser.setActivo(true);
			currentUser.setGoogle_id(googleUser.getUserId());
			currentUser.setGoogle_identity(googleUser.getFederatedIdentity());
			
			getUsuarioService().createUsuario(currentUser);
		}
		
		setRedirect(Home.class);
		
		return true;
	}
}
